package kz.kstu.scantest.business.common.util.comparator;

import kz.kstu.scantest.business.common.helper.FormDataHelper;
import kz.kstu.scantest.business.entity.data.FieldNameEnum;
import kz.kstu.scantest.business.entity.data.FormData;
import kz.kstu.scantest.business.entity.data.SubjectData;
import kz.kstu.scantest.business.entity.data.SubjectEnum;

import java.util.Comparator;

/**
 * @author Filipp_Stankevich
 *         created: 11.10.11 20:23
 */
public class FormDataFifthSubjectComparator implements Comparator<FormData> {

    private boolean isAscending;

    public FormDataFifthSubjectComparator() {
        this(true);
    }

    public FormDataFifthSubjectComparator(boolean isAscending) {
        this.isAscending = isAscending;
    }

    @Override
    public int compare(FormData o1, FormData o2) {
        if (o1 == null) {
            return o2 == null ? 0 : -1;
        }

        if (o2 == null) {
            return 1;
        }

        String firstSubjectName =  FormDataHelper.getFifthSubjectName(o1);
        String secondSubjectName =  FormDataHelper.getFifthSubjectName(o2);

        if (firstSubjectName == null) {
            return secondSubjectName == null ? 0 : -1;
        }

        if (secondSubjectName == null) {
            return 1;
        }

        return (isAscending ? 1 : -1) * firstSubjectName.compareTo(secondSubjectName);
    }
}
